package com.example.a001.part20;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TimePicker;


import com.example.a001.R;

import java.util.ArrayList;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    private EditText etDestination, etDepartureDate, etDepartureTime;
    private ListView lvOrders;
    private ArrayList<String> orderList;
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main7);

        etDestination = findViewById(R.id.et_destination);
        etDepartureDate = findViewById(R.id.et_departure_date);
        etDepartureTime = findViewById(R.id.et_departure_time);
        lvOrders = findViewById(R.id.lv_orders);

        orderList = new ArrayList<>();
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, orderList);
        lvOrders.setAdapter(adapter);
    }

    public void showDatePickerDialog(View v) {
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog datePickerDialog = new DatePickerDialog(
                this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        etDepartureDate.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
                    }
                },
                year, month, day);
        datePickerDialog.show();
    }

    public void showTimePickerDialog(View v) {
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        TimePickerDialog timePickerDialog = new TimePickerDialog(
                this,
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        etDepartureTime.setText(hourOfDay + ":" + minute);
                    }
                },
                hour, minute, true);
        timePickerDialog.show();
    }

    public void addOrder(View v) {
        String destination = etDestination.getText().toString();
        String departureDate = etDepartureDate.getText().toString();
        String departureTime = etDepartureTime.getText().toString();

        if (!destination.isEmpty() && !departureDate.isEmpty() && !departureTime.isEmpty()) {
            String order = "目的地: " + destination + ", 出发日期: " + departureDate + ", 出发时间: " + departureTime;
            orderList.add(order);
            adapter.notifyDataSetChanged();

            etDestination.setText("");
            etDepartureDate.setText("");
            etDepartureTime.setText("");
        }
    }

}